package com.ruoyi.jjm.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.jjm.mapper.ClientUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.jjm.mapper.GridMemeberMapper;
import com.ruoyi.jjm.domain.GridMemeber;
import com.ruoyi.jjm.service.IGridMemeberService;
import com.ruoyi.common.core.text.Convert;

/**
 * 网格员Service业务层处理
 * 
 * @author tsr
 * @date 2020-07-15
 */
@Service
public class GridMemeberServiceImpl implements IGridMemeberService 
{
    @Autowired
    private GridMemeberMapper gridMemeberMapper;

    /**
     * 查询网格员
     * 
     * @param id 网格员ID
     * @return 网格员
     */
    @Override
    public GridMemeber selectGridMemeberById(Long id)
    {
        return gridMemeberMapper.selectGridMemeberById(id);
    }

    /**
     * 查询网格员列表
     * 
     * @param gridMemeber 网格员
     * @return 网格员
     */
    @Override
    public List<GridMemeber> selectGridMemeberList(GridMemeber gridMemeber)
    {
        return gridMemeberMapper.selectGridMemeberList(gridMemeber);
    }

    /**
     * 新增网格员
     * 
     * @param gridMemeber 网格员
     * @return 结果
     */
    @Override
    public int insertGridMemeber(GridMemeber gridMemeber)
    {
        gridMemeber.setCreateTime(DateUtils.getNowDate());
        return gridMemeberMapper.insertGridMemeber(gridMemeber);
    }

    /**
     * 修改网格员
     * 
     * @param gridMemeber 网格员
     * @return 结果
     */
    @Override
    public int updateGridMemeber(GridMemeber gridMemeber)
    {
        gridMemeber.setUpdateTime(DateUtils.getNowDate());
        return gridMemeberMapper.updateGridMemeber(gridMemeber);
    }

    /**
     * 删除网格员对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteGridMemeberByIds(String ids)
    {
        return gridMemeberMapper.deleteGridMemeberByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除网格员信息
     * 
     * @param id 网格员ID
     * @return 结果
     */
    @Override
    public int deleteGridMemeberById(Long id)
    {
        return gridMemeberMapper.deleteGridMemeberById(id);
    }

    @Autowired
    ClientUserMapper clientUserMapper;

    @Override
    public GridMemeber insertOrUpdateGridMemeber(GridMemeber gridMemeber,Long clientUserId) {
        GridMemeber gm = gridMemeberMapper.selectGridMemeberByNameAndArea(gridMemeber);
        if(gm == null){
            gm = new GridMemeber();
            gm.setName(gridMemeber.getName());
            gm.setCreateBy(gridMemeber.getCreateBy());
            gm.setAreaId(gridMemeber.getAreaId());
            gm.setStatus(1);
            gm.setHouseholds(1);
            gridMemeberMapper.insertGridMemeber(gm);
        }else{
            int gridHouseholds = clientUserMapper.countGridById(gm.getId());
            if(clientUserId == null){
                gridHouseholds ++;
            }
            gm.setHouseholds(gridHouseholds);
            gridMemeberMapper.updateGridMemeber(gm);
        }
        return gm;
    }

}
